%by clq
%2021/6/12
function[i_RP,profits_RP] = round_robin_pref(j_all,pref)
global I J P
%j_all is the given order of patient types
addpath functions;
N = size(j_all,2);
S = zeros(I,J);
i_RP = zeros(1,N);
profits_RP = zeros(1,N);
%Step1:decide the preference randomly, and then find the optimal slot for each patient
j = 0;
k = 0;
Q = [ones(I,1),zeros(I,N)];
R = [ones(I,1),zeros(I,N)];

for n = 1:N
    pref_temp = pref(n);
    if pref_temp == 1
       j = j + 1;
       i_RP(n) = mod(j-1,I/2) + 1;
    else
       k = k+1;
       i_RP(n) = mod(k-1,I/2) + 5;
    end
end
%Step2:find the profit after each patient is assigned
for n = 1:N
    j = j_all(n);
    S(i_RP(n),j) = S(i_RP(n),j)+1;
    Q = findQ2(Q,i_RP(1,n),j,P,n); 
    R = findR2(Q,i_RP(1,n),R,n);
    profits_RP(n) = function_fQR2(Q,R,n);
end
end
